package com.aaron.kjviewutils.carte;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.aaron.kjviewutils.R;
import com.bumptech.glide.Glide;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;


import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 作者：Aaron on 2017/2/22 10:49
 * 邮箱：kaijin.fang@edianniu.com
 */
public class CarteRecyclerViewAdapter extends RecyclerView.Adapter<CarteRecyclerViewAdapter.CarteRecyclerViewHolder> {

    private Context context;
    private List<CarteDetailBean.CarteDetailInfo.CarteDetailInner> datas;

    public CarteRecyclerViewAdapter(Context context, List<CarteDetailBean.CarteDetailInfo.CarteDetailInner> datas)
    {
        this.context = context;
        this.datas = datas;
    }

    @Override
    public CarteRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.foot_item_layout, parent, false);
        return new CarteRecyclerViewHolder(view);
    }

    @Override
    public void onBindViewHolder(CarteRecyclerViewHolder holder, int position) {
        if(null!=datas&&!datas.isEmpty())
        {
            CarteDetailBean.CarteDetailInfo.CarteDetailInner inner = datas.get(position);
            holder.footType.setText(inner.ctgTitles);
            holder.footSubType.setText(inner.name);
            CarteDetailBean.CarteDetailInfo.CarteDetailInner.Recipe recipe = inner.recipe;
            holder.footTitle.setText(recipe.title);
            holder.sumary.setText(recipe.sumary);
            if(null!=recipe.ingredients)
            {
                JsonParser parser = new JsonParser();
                JsonArray jsonArray = parser.parse(recipe.ingredients).getAsJsonArray();
                int size = jsonArray.size();
                if(size>0)
                {
                    StringBuilder builder = new StringBuilder();
                    for(int i=0; i<size; i++)
                    {
                        builder.append(jsonArray.get(i).getAsString());
                    }
                    holder.ingredients.setText(builder.toString());
                }
            }
            if(null!=recipe.method)
            {
                JsonParser parser = new JsonParser();
                JsonArray jsonArray = parser.parse(recipe.method).getAsJsonArray();
                int size = jsonArray.size();
                if(size>0)
                {
                    holder.methodL.removeAllViews();
                    for(int i=0; i<size; i++)
                    {
                        JsonObject data = jsonArray.get(i).getAsJsonObject();
                        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup
                                .LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                        RelativeLayout methodLayout = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.item_foot_inner, null);
                        TextView mStep = (TextView) methodLayout.findViewById(R.id.mStep);
                        ImageView mIcon = (ImageView) methodLayout.findViewById(R.id.mIcon);
                        mStep.setText((null==data.get("step"))?"未知步骤":data.get("step").getAsString());
                        Glide.with(context).load((null==data.get("img"))?"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1488349116&di=70ac8d08f0488483c0d696cfd8d0afe2&imgtype=jpg&er=1&src=http%3A%2F%2Fa0.att.hudong.com%2F77%2F31%2F20300542906611142174319458811.jpg":data.get("img").getAsString()).dontAnimate().centerCrop().placeholder(R.mipmap.ic_launcher).into(mIcon);
                        methodLayout.setLayoutParams(lp);
                        holder.methodL.addView(methodLayout);
                    }
                }
            }
        }
    }

    @Override
    public int getItemCount() {
        return (null==datas)?0:datas.size();
    }

    public static class CarteRecyclerViewHolder extends RecyclerView.ViewHolder
    {
        @Bind(R.id.footType)
        TextView footType;
        @Bind(R.id.footSubType)
        TextView footSubType;
        @Bind(R.id.footTitle)
        TextView footTitle;
        @Bind(R.id.ingredients)
        TextView ingredients;
        @Bind(R.id.methodL)
        LinearLayout methodL;
        @Bind(R.id.sumary)
        TextView sumary;

        public CarteRecyclerViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
